package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.admin.pojos.AdUser;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmUserDto;
import com.usian.model.media.pojos.WmUser;
import com.usian.utils.common.AppJwtUtil;
import com.usian.wemedia.mapper.WmUserMapper;
import com.usian.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    @Override
    public ResponseResult add(WmUser wmUser) {
        //判断参数是否有效
        if (wmUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmUser.setCreatedTime(new Date());
        boolean b = this.save(wmUser);
        if (b){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

    }

    @Override
    public WmUser findByName(String name) {
        if (StringUtils.isBlank(name)){
            return null;
        }
        List<WmUser> wmUserList = this.list(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        if (wmUserList.size()>0){
            return wmUserList.get(0);
        }
        return null;
    }

    @Override
    public ResponseResult login(WmUserDto dto) {
        //校验参数
        if (dto==null||StringUtils.isBlank(dto.getName())|| StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据用户名去数据库查询
        QueryWrapper<WmUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(WmUser::getName,dto.getName());
        List<WmUser> list = this.list(queryWrapper);
        if (list.size()>0){
            WmUser wmUser = list.get(0);
            //用户输入密码+数据库中查出来的盐值 生产一个新的MD5密码串
            String newPwd = DigestUtils.md5DigestAsHex(dto.getPassword().concat(wmUser.getSalt()).getBytes());

            //数据库中密码串比较 生成一个新的MD5密码串 如果是true生成token 传到前台
            if (wmUser.getPassword().equals(newPwd)){
                //生成token
                Map<String,Object> map=new HashMap<>();
                map.put("token", AppJwtUtil.getToken(wmUser.getId().longValue()));
                wmUser.setPassword("");
                wmUser.setSalt("");
                map.put("user",wmUser);
                return ResponseResult.okResult(map);
            }else {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
    }



    @Override
    public WmUser findByUserId(Integer userId) {

        return this.getById(userId);
    }
}
